package com.gjzt.business.media.service;

import net.sf.json.JSONArray;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.system.model.BusinessException;
import com.eastdigit.system.model.SqlBuilder;
import com.eastdigit.system.service.BaseTreeService;
import com.eastdigit.util.StringUtil;
import com.gjzt.business.media.inter.MediaMarkTypeMapper;
import com.gjzt.business.media.model.MediaMarkType;

@Service
public class MediaMarkTypeService extends BaseTreeService {

    @Autowired
    private MediaMarkTypeMapper mediaMarkTypeMapper;

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    public JSONArray findMediaMarkType(ReqBean reqBean) {
        String parentId = reqBean.getInfo("parentId");
        if(StringUtils.isBlank(parentId)) {
            throw new BusinessException("缺少参数!");
        }
        SqlBuilder sqlBuilder = new SqlBuilder("select * from MEDIA_MARK_TYPE a where 1=1 ").addWhere("a.PARENT_ID", parentId);
        return this.query(sqlBuilder);
    }

    /**
     * 新增
     *
     * @param reqBean
     */
    public void addMediaMarkType(ReqBean reqBean) {
        MediaMarkType mediaMarkType = getMediaMarkType(reqBean);
        mediaMarkType.setMarkId(intIncrementId("MEDIA_MARK_TYPE","MARK_ID"));
        mediaMarkTypeMapper.insert(mediaMarkType);
    }

    /**
     * 修改
     *
     * @param reqBean
     */
    public void updateMediaMarkType(ReqBean reqBean) {
        String markId = reqBean.getInfo("markId");
        if (StringUtil.isEmpty(markId)) {
            throw new BusinessException("获取主键失败");
        }
        MediaMarkType mediaMarkType = getMediaMarkType(reqBean);
        mediaMarkTypeMapper.updateByPrimaryKeySelective(mediaMarkType);
    }

    /**
     * 删除
     *
     * @param reqBean
     */
    public void removeMediaMarkType(ReqBean reqBean) {
        String markId = reqBean.getInfo("markId");
        if (StringUtil.isEmpty(markId)) {
            throw new BusinessException("获取主键失败");
        }
        mediaMarkTypeMapper.deleteByPrimaryKey(markId);
    }

    /**
     * 从表单获取对象
     *
     * @param reqBean
     * @return
     */
    private MediaMarkType getMediaMarkType(ReqBean reqBean) {
        MediaMarkType mediaMarkType = new MediaMarkType();

        mediaMarkType.setMarkId(reqBean.getInfo("markId"));
        mediaMarkType.setMarkName(reqBean.getInfo("markName"));
        mediaMarkType.setParentId(reqBean.getInfo("parentId"));

        return mediaMarkType;
    }

    /**
     * 商圈树
     * @param reqBean
     * @return
     */
    public JSONArray queryTree(ReqBean reqBean) {
        String sql = "SELECT * FROM `MEDIA_MARK_TYPE`";
        return queryTreeGrid("markId","parentId",query(sql));
    }

    /**
     * 查询顶层分类
     * @param reqBean
     * @return
     */
    public JSONArray queryTopType(ReqBean reqBean) {
        String sql = "SELECT a.*,a.MARK_ID `order` FROM `MEDIA_MARK_TYPE` a where a.PARENT_ID = '0'";
        return query(sql);
    }
}